function [mat_X_TA, residual, mat_Y_TA, price_clo, price_hi,...
    price_lo, dates_ret] = prepare_TA(stock_id, data_FA, ...
    ret_FA, gap_TA, gap_ret, window, time_lag)

mat_X_TA = [];
mat_Y_TA = [];
residual = [];
price_clo = [];
price_hi = [];
price_lo = [];
dates_ret = [];

year_TA = data_FA(:,2);
quar_TA = data_FA(:,3);

for i = 1: size(data_FA,1)
    switch time_lag
        case 1
    if  quar_TA(i) <=3
        quar_TA1 = quar_TA(i) + 1;
        year_TA1 = year_TA(i);
        
        quar_TA_pre = quar_TA(i);
        year_TA_pre = year_TA(i);
    else
        quar_TA1 = quar_TA(i) - 3;
        year_TA1 = year_TA(i) + 1;
        
        quar_TA_pre = quar_TA(i);
        year_TA_pre = year_TA(i);
    end
           
        case 2
    if  quar_TA(i) <=2
        quar_TA1 = quar_TA(i) + 2;
        year_TA1 = year_TA(i);
        
        quar_TA_pre = quar_TA(i) +1;
        year_TA_pre = year_TA(i);
        
    elseif quar_TA(i) == 3
        quar_TA1 = quar_TA(i) - 2;
        year_TA1 = year_TA(i) + 1;
        
        quar_TA_pre = quar_TA(i) + 1;
        year_TA_pre = year_TA(i);
    else
        quar_TA1 = quar_TA(i) - 2;
        year_TA1 = year_TA(i) + 1;
        
        quar_TA_pre = quar_TA(i) - 3;
        year_TA_pre = year_TA(i) + 1;
    end
    end
    
    rslt = get_info_from_database('prices_in_quarter',...
        stock_id, year_TA1, quar_TA1);
    rslt_pre = get_info_from_database('prices_in_quarter',...
        stock_id, year_TA_pre, quar_TA_pre);
    rslt_TA = vertcat(rslt_pre(end - window + 1:end,:),rslt);
    rslt_dates = rslt_TA(:,1);
    rslt_TA = cell2mat(rslt_TA(:,2:end));
        
    idx_gap = 1:gap_TA:size(rslt_TA,1)- window - gap_ret +1;
    
    for k = 1:length(idx_gap)
        rslt_gap = rslt_TA(idx_gap(k):idx_gap(k)+ window + gap_ret-1,:);
        dates = rslt_dates(idx_gap(k):idx_gap(k)+ window + gap_ret-1,:);
        
        volume = rslt_gap(:,1);
        hi     = rslt_gap(:,3);
        lo     = rslt_gap(:,4);
        clo    = rslt_gap(:,5);
        
        [pattern] = patternRec(clo(1:window),window);
        emaShort  = indicators(clo(1:window),'ema',7);
        emaLong  = indicators(clo(1:window),'ema',34);
        macdArr = indicators(clo(1:window),'macd',12,26,9);
        adxArr = indicators([hi(1:window),lo(1:window),...
            clo(1:window)],'adx',14);
        cci = indicators([hi(1:window),lo(1:window),...
            clo(1:window)],'cci',14,20,0.15);
        roc = indicators(clo(1:window),'roc',15);
        rsi = indicators(clo(1:window),'rsi',14);
        willr = indicators([hi(1:window),lo(1:window),...
            clo(1:window)],'william',14);
        
        matX = [volume(1:window) clo(1:window) emaShort...
            emaLong macdArr adxArr cci roc rsi willr];
        
        % Compute the mean/median/mode
        matX = mean(matX(35:end,:),1);
        mat_X = [pattern matX];
        
        mat_Y = ((clo(end) - clo(end - gap_ret +1))...
            /clo((end - gap_ret +1)))*100;
        res = mat_Y - ret_FA(i);
        
        mat_X_TA = [mat_X_TA ; mat_X];
        mat_Y_TA = [mat_Y_TA ; mat_Y];
        residual = [residual ; res];
        
        pri_clo = [clo(end - gap_ret +1) ; clo(end)];
        price_clo = [price_clo ; pri_clo];
        
        pri_hi = [hi(end - gap_ret +1) ; hi(end)];
        price_hi = [price_hi ; pri_hi];
        
        pri_lo = [lo(end - gap_ret +1) ; lo(end)];
        price_lo = [price_lo ; pri_lo];
        
        dat = [dates(end - gap_ret +1) ; dates(end)];
        dates_ret = [dates_ret ; dat];
    end
end

end
